package com.opensky.qatest.ui.page;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.ui.component.LoginPageSignInForm;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.common.Properties;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Login page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.1
 * 
 */
public class LoginPage extends LoadableComponent<LoginPage> implements IPage {

    // ************************ Locators ************************

    private static final String LOGIN_PAGE_URL = Properties.get("env.root.url") + "login";

    private static final String LOGOUT_URL = Properties.get("env.root.url") + "logout";

    private static final String LOGO = "//header//img[@class='logo']";

    private static final String COPYRIGHT_CONTAINER = "//div[@class='copyright']";

    private static final String COPYRIGHT_TEXT = "2011 The OpenSky Project, Inc.";

    private static final String JOIN_BTN_LINK = "//a[@class='button join-opensky']";

    private static final String FACEBOOK_LINK = "Log In With Facebook";

    // ************************ Complex page objects **************************

    private LoginPageSignInForm loginPageSignInForm;

    private final WebDriverWrapper driver;

    public LoginPage(WebDriverWrapper driver) {
	this.driver = driver;
	loginPageSignInForm = new LoginPageSignInForm(driver);
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(LOGIN_PAGE_URL);
    }

    public LoginPage logOut() {
	driver.get(LOGOUT_URL);
	return this.isPageLoaded() ? this : null;
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(LOGIN_PAGE_URL, driver.getCurrentUrl());
    }

    @Override
    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(LOGIN_PAGE_URL);
    }

    public boolean isLogoAndCopyrightPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("LOGO", By.xpath(LOGO)),
		driver.waitAndVerifyElementWithTextPresent("COPYRIGHT_CONTAINER", By.xpath(COPYRIGHT_CONTAINER), COPYRIGHT_TEXT));
    }

    public boolean isJoinBtnAndFacebookLinkPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("JOIN_BTN_LINK", By.xpath(JOIN_BTN_LINK)),
		driver.waitAndVerifyElementPresent("FACEBOOK_LINK", By.linkText(FACEBOOK_LINK)));
    }

    public boolean isLogedOut() {
	return LogicUtils.isURLEqual(driver.getCurrentUrl(), LOGIN_PAGE_URL);
    }

    // ************************ GET/SET methods **************************

    public LoginPageSignInForm getSignInForm() {
	return loginPageSignInForm.isComponentLoaded() ? loginPageSignInForm : null;
    }

    @Override
    public String getName() {
	return "Login page";
    }
}
